package com.jilijili.controller;


import com.jilijili.enums.YesOrNo;
import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.grace.result.ResponseStatusEnum;
import com.jilijili.pojo.bo.VlogBo;
import com.jilijili.pojo.vo.IndexVlogVO;
import com.jilijili.service.VlogService;
import com.jilijili.service.base.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@Api(value = "VlogController 视频控制器", tags = "视频管理接口模块", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/vlog")
@RefreshScope
public class VlogController extends BaseInfoProperties {

    @Resource
    private VlogService vlogService;

    @Value("${nacos.counts}")
    public Long nacosCounts;

    /**
     * 我的關注視頻列表
     *
     * @param page
     * @param pageSize
     * @return com.jilijili.grace.result.GraceJSONResult
     */
    @ApiOperation(value = "我的關注視頻列表")
    @GetMapping(value = "/friendList")
    public GraceJSONResult friendList(@RequestParam(value = "myId", required = true) String myId,
                                      @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        PagedGridResult myFriendVlogList = this.vlogService.getMyFriendVlogList(myId, page, pageSize);

        return GraceJSONResult.ok(myFriendVlogList);
    }


    /**
     * 我的關注視頻列表
     *
     * @param myId
     * @param page
     * @param pageSize
     * @return com.jilijili.grace.result.GraceJSONResult
     */
    @ApiOperation(value = "我的關注視頻列表")
    @GetMapping(value = "/followList")
    public GraceJSONResult followList(@RequestParam(value = "myId", required = true) String myId,
                                      @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult myLinkVlogList = this.vlogService.getMyFollowVlog(myId, page, pageSize);

        return GraceJSONResult.ok(myLinkVlogList);
    }

    /**
     * 查询当前点赞数
     *
     * @param vlogId
     * @return
     */
    @ApiOperation(value = "查询当前点赞数，重新赋值给当前视频")
    @PostMapping(value = "/totalLikedCounts")
    public GraceJSONResult totalLikedCounts(@RequestParam(value = "vlogId", required = true) String vlogId) {

        Integer vlogBeLikedCounts = this.vlogService.getVlogBeLikedCounts(vlogId);

        return GraceJSONResult.ok(vlogBeLikedCounts);

    }

    //    unlike
    @ApiOperation(value = "我取消点赞视频")
    @PostMapping(value = "/unlike")
    public GraceJSONResult unlike(@RequestParam(value = "userId", required = true)
                                          String userId,
                                  @RequestParam(value = "vlogerId", required = true)
                                          String vlogerId,
                                  @RequestParam(value = "vlogId", required = true)
                                          String vlogId) {
        //TODO 取消点赞关系, 关联关系保存到数据库中(用户和视频)
        this.vlogService.userUnLikeVlog(userId, vlogId);

        //TODO 视频和作者获赞-1
        this.redis.decrement(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + vlogerId, 1);
        this.redis.decrement(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId, 1);

        //TODO 我取消点赞需要在redis中保存关联关系
        this.redis.del(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId);


        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "我点赞的视频")
    @PostMapping(value = "/like")
    public GraceJSONResult myLikedList(@RequestParam(value = "userId", required = true)
                                               String userId,
                                       @RequestParam(value = "vlogerId", required = true)
                                               String vlogerId,
                                       @RequestParam(value = "vlogId", required = true)
                                               String vlogId) {
        //TODO 点赞关系, 关联关系保存到数据库中(用户和视频)
        this.vlogService.userLikeVlog(userId, vlogId);

        //TODO 视频和作者获赞+1
        this.redis.increment(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + vlogerId, 1);
        long counts = this.redis.increment(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId, 1);

        //TODO 我点赞需要在redis中保存关联关系
        this.redis.set(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId, "1");

        // 成功点赞,获取点赞数量
        // 根据阈值配置为2000
        // 如果满足,触发入库
        log.info("{}=>视频 {} 被点赞的数量:{}", Thread.currentThread().getName(), vlogId, counts);
        if (counts >= 0) {
            if (counts >= this.nacosCounts) {
                this.vlogService.flushCounts(vlogId, counts);
            }

        }


        return GraceJSONResult.ok();
    }

    /**
     * 查詢獲讚列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return com.jilijili.grace.result.GraceJSONResult
     */
    @ApiOperation(value = "我的獲讚")
    @GetMapping(value = "/myLikedList")
    public GraceJSONResult myLikedList(@RequestParam(value = "userId", required = true) String userId,
                                       @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult myLinkVlogList = this.vlogService.getMyLinkVlogList(userId, page, pageSize);

        return GraceJSONResult.ok(myLinkVlogList);

    }

    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "我的私密")
    @GetMapping(value = "/myPrivateList")
    public GraceJSONResult myPrivateList(@RequestParam(value = "userId", required = true) String userId,
                                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PagedGridResult pagedGridResult = this.vlogService.queryMyVlogList(userId, page, pageSize, YesOrNo.YES.type);
        return GraceJSONResult.ok(pagedGridResult);

    }

    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "我的作品")
    @GetMapping(value = "/myPublicList")
    public GraceJSONResult myPublicList(@RequestParam(value = "userId", required = true) String userId,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PagedGridResult pagedGridResult = this.vlogService.queryMyVlogList(userId, page, pageSize, YesOrNo.NO.type);
        return GraceJSONResult.ok(pagedGridResult);
    }


    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "视频设置为公开")
    @PutMapping(value = "/changeToPublic")
    public GraceJSONResult changeToPrivateOrPublic(@RequestParam(value = "userId", required = true) String userId,
                                                   @RequestParam(value = "vlogId", required = true) String vlogId) {

        this.vlogService.changeToPrivateOrPublic(userId, vlogId, YesOrNo.NO.type);
        return GraceJSONResult.ok(ResponseStatusEnum.SUCCESS);
    }

    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "视频设置为私有")
    @PutMapping(value = "/changeToPrivate")
    public GraceJSONResult changeToPrivate(@RequestParam(value = "userId", required = true) String userId,
                                           @RequestParam(value = "vlogId", required = true) String vlogId) {
        this.vlogService.changeToPrivateOrPublic(userId, vlogId, YesOrNo.YES.type);
        return GraceJSONResult.ok(ResponseStatusEnum.SUCCESS);
    }


    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "视频详情")
    @GetMapping(value = "/detail")
    public GraceJSONResult detail(@RequestParam(value = "userId", defaultValue = "") String userId,
                                  @RequestParam("vlogId") String vlogId) {
        log.warn("{}--->/api/vlog/detail", "userId = " + userId + ", vlogId = " + vlogId);
        IndexVlogVO indexVlogVO = vlogService.getVlogDetailById(userId, vlogId);
        return GraceJSONResult.ok(indexVlogVO);

        // 根据VlogId查询视频详情


    }

    // 首页视频展示|搜索展示
    @ApiImplicitParams({
            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "首页视频展示|搜索展示")
    @GetMapping("/indexList")
    public GraceJSONResult index(@RequestParam(value = "search", defaultValue = "", required = false) String searchKeyword,
                                 @RequestParam(value = "userId", defaultValue = "") String userId,
                                 @RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        if (page == null || pageSize == null) {
            page = COMMON_START_PAGE;
            pageSize = COMMON_PAGE_SIZE;
        }

        //TODO 查询|搜索
        PagedGridResult pagedGridResult = this.vlogService.getIndexVlogList(searchKeyword, userId, page, pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }

    // 发布视屏
//    @ApiImplicitParams({
//            //@ApiImplicitParam(name = "id", value = "用户id", required = true),
//    })
    @ApiOperation(value = "发布视屏")
    @PostMapping("/publish")
    public GraceJSONResult saveVlog(@RequestBody @Valid @ApiParam(name = "视频传输对象", required = true)
                                            VlogBo vlogBo) {

        //TODO 新增视频操作
        this.vlogService.createVlog(vlogBo);

        return GraceJSONResult.ok();
    }
}
